package com.sqm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqm.config.BaseContext;
import com.sqm.entity.Shujia;
import com.sqm.mapper.ShujiaMapper;
import com.sqm.model.Novel;
import com.sqm.service.ShujaService;
import jakarta.annotation.Resource;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 沙琪马
 * 日期: 2024/6/12 17:40
 */

@Service
public class ShujiaServiceImpl extends ServiceImpl<ShujiaMapper, Shujia> implements ShujaService {

    @Resource
    private ShujiaMapper shujiaMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 根据用户名查询书架
     * @param id
     * @return
     */
    @Override
    public List<Novel> getByName(String id) {
        // 1.构建查询条件
        // 1.1.根据用户id
        LambdaQueryWrapper<Shujia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shujia::getName, id);
        List<Shujia> shujias = shujiaMapper.selectList(queryWrapper);
        List<Novel> novels = new ArrayList<>();
        shujias.forEach(shujia -> {
            Query query = new Query(new Criteria("_id").is(shujia.getShuId()));
            novels.add(mongoTemplate.findOne(query,Novel.class));
        });

        return novels;
    }

    /**
     * 添加书架
     * @param shujia
     */
    @Override
    public void add(Shujia shujia) {
        Integer currentId = BaseContext.getCurrentId();
        shujia.setName(currentId.toString());
        saveOrUpdate(shujia);
    }

    @Override
    public List<Map<String, Object>> getChars() {
        // 1.返回所有人shujia中的shu_id
        QueryWrapper<Shujia> queryWrapper = new QueryWrapper<>();
        // Construct the SQL clause: SELECT type, COUNT(*) as count FROM item GROUP BY type
        queryWrapper.select("type", "COUNT(*) AS count").groupBy("type");

        List<Map<String, Object>> result = shujiaMapper.selectMaps(queryWrapper);

        return result;
    }

    @Override
    public Boolean getShujiaExists(String id) {
        Integer currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Shujia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shujia::getName, currentId.toString())
                .eq(Shujia::getShuId, id);
        return shujiaMapper.selectCount(queryWrapper) != 0;
    }

    @Override
    public void deleteShujia(String id) {
        Integer currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Shujia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Shujia::getName, currentId.toString())
                .eq(Shujia::getShuId, id);
        shujiaMapper.delete(queryWrapper);
    }

}
